const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const http = require('http');
const cors = require('cors');


//学生
const stuLoginRouter = require('./routes/login');//登录路由
const resetPassword = require('./routes/student/resetPassword');//重置密码路由
const registerRouter = require('./routes/student/register');//注册路由
const applyCreditRouter = require('./routes/student/applyCredit');//学分申请列表路由
const getProfileRouter = require('./routes/student/getProfile');//获取学生个人信息路由
const getCreditRecord = require('./routes/student/getCreditRecord');//获取学生学分详情（已通过的）
const getApply = require('./routes/student/getApply')//获取学生申请学分记录
const getApplyList = require('./routes/student/getApplyList')
const getFeedBack = require('./routes/student/getFeedBack')//获取学生申请学分记录
const applyStudentFeedback = require('./routes/student/applyStudentFeedback')//反馈
const editStudentProfile = require('./routes/student/editStudentProfile')//修改个人信息

//管理
const getAllLocalClass = require('./routes/admin/getLocalAllClass');//获取所有班级
const getClassApply = require('./routes/admin/getClassApply');//获取班级申请
const getClassFeedback = require('./routes/admin/getClassFeedback');//获取班级反馈记录
const getLocalPartySecretary = require('./routes/admin/getLocalPartySecretary');//获取团支书列表
const getLocalStudentOfThePS = require('./routes/admin/getLocalStudentOfThePS');//获取此团支书下的学生列表
const getLocalCreditRecordOfStu = require('./routes/admin/getLocalCreditRecordOfStu');//获取团支书所属学生的学分详情（已通过的）
const AddPartySecretary = require('./routes/admin/AddPartySecretary');//添加团支书
const DeleteThePartySecretary = require('./routes/admin/DeleteThePartySecretary');//删除团支书
const ModifyThePartySecretary = require('./routes/admin/ModifyThePartySecretary');//修改团支书
const studentaddRouter = require('./routes/admin/studentadd');//管理添加学生路由
const studentlist = require('./routes/admin/studentlist');//管理展示学生列表路由
const studentdel = require('./routes/admin/studentdel');//管理删除学生（假删除）
const studentupdate = require('./routes/admin/studentupdate');//管理修改学生信息（假删除）
const getStudentInfo = require('./routes/admin/getStudentInfo');//管理获取学生信息
//团支书
const AgreeToStudentRequest = require('./routes/manage/AgreeToStudentRequest');//获取本班申请
const getLocalProfile = require('./routes/manage/getLocalProfile');//获取个人信息
const editLocalProfile = require('./routes/manage/editLocalProfile');//修改个人信息
const DenyStudentRequest = require('./routes/manage/DenyStudentRequest');//获取本班申请
const getAllApply = require('./routes/manage/getAllApply');//获取本班申请
const getAllFeedBack = require('./routes/manage/getAllFeedBack');//获取本班申请
const getLocalStudents = require('./routes/manage/getLocalStudents');//获取本班申请
const ReplyToStuReplyFeedback = require('./routes/manage/ReplyToStuReplyFeedback')
const getLocalStudentCreditRecord = require('./routes/manage/getLocalStudentCreditRecord');//获取所属学生学分记录
const getLocalStudentAgreeAllApply = require('./routes/manage/getLocalStudentAgreeAllApply')//获取所属单个学生的学分详情
const IgnoreStudentFeedback = require('./routes/manage/IgnoreStudentFeedback')
const app = express();
//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    next();
});


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(cors());
app.use(logger('dev'));
// app.use(formidable());
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


//学生路由
app.use('/login', stuLoginRouter);
app.use('/login/resetPassword', resetPassword);
app.use('/register', registerRouter);
app.use('/student/applyCredit', applyCreditRouter);
app.use('/student/getProfile', getProfileRouter);
app.use('/student/getApply', getApply);
app.use('/student/getApplyList', getApplyList);
app.use('/student/getCreditRecord', getCreditRecord);
app.use('/student/getFeedBack', getFeedBack)
app.use('/student/applyStudentFeedback', applyStudentFeedback)
app.use('/student/editStudentProfile', editStudentProfile);
//管理路由
app.use('/getAllClass', getAllLocalClass);
app.use('/getClassApply', getClassApply);
app.use('/getClassFeedback', getClassFeedback);
app.use('/getLocalPartySecretary', getLocalPartySecretary);
app.use('/getLocalCreditRecordOfStu', getLocalCreditRecordOfStu);
app.use('/getLocalStudentOfThePS', getLocalStudentOfThePS);
app.use('/AddPartySecretary', AddPartySecretary);
app.use('/DeleteThePartySecretary', DeleteThePartySecretary);
app.use('/ModifyThePartySecretary', ModifyThePartySecretary);
app.use('/studentadd', studentaddRouter);
app.use('/studentlist', studentlist);
app.use('/studentdel', studentdel);
app.use('/studentupdate', studentupdate);
app.use('/getStudentInfo', getStudentInfo);
//团支书路由
app.use('/manager/AgreeToStudentRequest', AgreeToStudentRequest);
app.use('/manager/getLocalProfile', getLocalProfile);
app.use('/manager/editLocalProfile', editLocalProfile);
app.use('/manager/DenyStudentRequest', DenyStudentRequest);
app.use('/manager/getAllApply', getAllApply);
app.use('/manager/getAllFeedBack', getAllFeedBack);
app.use('/manager/getLocalStudents', getLocalStudents);
app.use('/manager/ReplyToStuReplyFeedback', ReplyToStuReplyFeedback);
app.use('/manager/getLocalStudentCreditRecord', getLocalStudentCreditRecord);
app.use('/manager/getLocalStudentAgreeAllApply', getLocalStudentAgreeAllApply);
app.use('/manager/IgnoreStudentFeedback', IgnoreStudentFeedback);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});


module.exports = app;
